import 'dart:io';

import 'package:FineArt/page/splash/splash.dart';
import 'package:FineArt/utils/log_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:FineArt/core/http/http.dart';
import 'package:FineArt/core/utils/toast.dart';
import 'package:FineArt/generated/i18n.dart';
import 'package:FineArt/router/router.dart';
import 'package:FineArt/utils/provider.dart';
import 'package:FineArt/utils/sputils.dart';
import 'package:live_plugin/features/me/bloc/me_bloc.dart';
import 'package:live_plugin/features/media_devices/bloc/media_devices_bloc.dart';
import 'package:live_plugin/features/peers/bloc/peers_bloc.dart';
import 'package:live_plugin/features/producers/bloc/producers_bloc.dart';
import 'package:live_plugin/features/room/bloc/room_bloc.dart';
import 'package:live_plugin/features/signaling/room_client_repository.dart';
import 'package:live_plugin/screens/room/room.dart';
import 'package:live_plugin/screens/room/room_modules.dart';
import 'package:live_plugin/screens/welcome/welcome.dart';
import 'package:provider/provider.dart';

import 'navigation_service.dart';
import 'package:random_string/random_string.dart';


//默认App的启动
class DefaultApp {
  //运行app
  static void run() {
    WidgetsFlutterBinding.ensureInitialized();
    if (Platform.isAndroid) {
      // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
      // SystemUiOverlayStyle systemUiOverlayStyle =
      // SystemUiOverlayStyle(statusBarColor: Colors.transparent);
      // SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }
    initApp();
    SPUtils.init()
        .then((value) => runApp(Store.init(ToastUtils.init(MyApp()))));

  }

  //程序初始化操作
  static void initApp() {
    XHttp.init();
    XRouter.init();
  }
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    LogUtil.d("myapp >>>>>");
    return Consumer2<AppTheme, LocaleModel>(
        builder: (context, appTheme, localeModel, _) {
      return MaterialApp(
        navigatorKey: NavigationService.navigatorKey,
        debugShowCheckedModeBanner: false,
        title: 'FineArtCloud',
        theme: ThemeData(
          primarySwatch: appTheme.themeColor,
          buttonColor: appTheme.themeColor,
          highlightColor: Colors.transparent,
          splashColor: Colors.transparent,
        ),
        //home: ChooseRole(),
        home: SplashPage(),
        // routes: RouterMap.pagesMap,
        // builder: ExtendedNavigator<RouterMap>(
        //   router: RouterMap(),
        //   guards: [AuthGuard()],
        // ),
        locale: localeModel.getLocale(),
        supportedLocales: I18n.delegate.supportedLocales,
        localizationsDelegates: [
          I18n.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        localeResolutionCallback:
            (Locale _locale, Iterable<Locale> supportedLocales) {
          if (localeModel.getLocale() != null) {
            //如果已经选定语言，则不跟随系统
            return localeModel.getLocale();
          } else {
            //跟随系统
            if (I18n.delegate.isSupported(_locale)) {
              return _locale;
            }
            return supportedLocales.first;
          }
        },
      );
    });
  }
}
